#!/bin/sh
#
# Author : Brendan Horan
# Description : To load and unload the ds1338 i2c rtc
# you should only need to change the address
#
# chkconfig: 12345 82 82
# description: I2C RTC

# Source function library.
if [ -e /etc/rc.d/init.d/functions ]; then
  . /etc/rc.d/init.d/functions
fi

# set the hex address of the rtc chip
address=0x68

# Change nothing below this line
RETVAL=0


start() {
        echo -n "Starting I2C RTC : "
        modprobe i2c-dev
        echo ds1307 ${address} > /sys/class/i2c-adapter/i2c-0/new_device
	RETVAL=$?
        touch /var/lock/subsys/pi-rtc
	if [ ${RETVAL} -eq 0 ]
	then
	 success
	else
	 failure
	fi
        echo 
        return $RETVAL
}

stop() {
        echo -n "Stopping I2C RTC : "
        echo ${address} > /sys/class/i2c-adapter/i2c-0/delete_device
	RETVAL=$?
        rmmod rtc_ds1307
        rmmod i2c-dev
        rm -f /var/lock/subsys/pi-rtc
        if [ $RETVAL -eq 0 ]
        then
         success
        else
         failure
        fi

        echo
        return $RETVAL
}


case "$1" in
  start)
	start
        ;;
  stop)
	stop
        ;;
  *)
        echo "Usage: pi-rtc {start|stop}"
        exit 1
esac

exit $RETVAL
